% Change to project folder
cd('C:\Users\Dani\Desktop\CV project\Assignment')

% CONSTANTS
descriptorN = 20;
cupThreshold = 20;
sceneThreshold = 25;

% We read the image
cup = imread('cup.png');

% First, let's see the histogram of the image
imhist(cup)
axis([0 255 0 80])

% SEGMENTATION AND CLEANING UP

% We need to segment the image from the background, using basic (threshold = 25)
tcup = basicThresholding(cup, cupThreshold);
% Let's try with erosion
erodeCup = imerode(tcup, strel('square', 3));

% INNER BORDER TRACING BORDER

% Inner boundary tracing algorithm
cupBorder = innerBorder(erodeCup, 1);

% CUP SHAPE DESCRIPTOR

% First we calculate the complex coordinates of the border vector 
cupDescriptor = getDescriptor(cupBorder, descriptorN);
 
 % SCENE
 
 % Segmentation and cleaning up of the scene
scene = imread('scene.png');
imhist(scene);
axis([0 255 0 900])
tscene = basicThresholding(scene, sceneThreshold);
erodeScene = imerode(tscene, strel('diamond', 3));
finalScene = imdilate(erodeScene, strel('square', 3));

% Now we separate and label the objects
[objects num ] = bwlabel(finalScene);

% We compute the borders and then the descriptors
sceneDescriptors = [];
for currentObject = 1:num
	currentBorder = innerBorder(objects, currentObject);
	sceneDescriptors = [sceneDescriptors ; getDescriptor(currentBorder, descriptorN)];
end

% OBJECT RECOGNITION

% We compute the Euclidean distance for each object of the scene
distances = [];
for currentObject = 1:num
	currentDistance = norm(sceneDescriptors(currentObject,:) - cupDescriptor);
	distances = [distances ; currentDistance];
end

